package com.woniuxy.main.service.impl;

import com.woniuxy.main.mapper.OrderMapper;
import com.woniuxy.main.mapper.RoomMapper;
import com.woniuxy.main.pojo.Room;
import com.woniuxy.main.service.RoomService;
import com.woniuxy.main.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class RoomServiceImp implements RoomService {

    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Override
    public List<Room> loadAllRoom() {

        return roomMapper.loadAllRoom();
    }

    @Override
    public List<Room> searchRoom(Room room) {

        return roomMapper.searchRoom(room);
    }

    @Override
    public boolean addRoom(Room room) {


        return roomMapper.addRoom(room);
    }

    @Override
    public boolean delectRoom(int room_id) {

        return roomMapper.delectRoom(room_id);
    }

    @Override
    public boolean updateStatus(int status, int room_id) {

        return roomMapper.updateStatus(status, room_id);
    }

    @Override
    public boolean updateCut(double cut) {

        return roomMapper.updateCut(cut);
    }

    @Override
    public List<Room> availableRoom() throws Exception {
        //1.获取今日时间 逻辑时间为动态
        String dateToString = DateUtils.formatDateToString(new Date(), DateUtils.DATE_FORMAT_YMD);
        //2.得到 拟今日入住的所有订单号
        List<Integer> orderIdList = orderMapper.findByOrderdate("%" + dateToString + "%");
        //3.获取今日所有订单号 中预定的房间对象
		//3.1判断今日是否有预定订单
        if (orderIdList.size() != 0) {
            List<Room> roomIdList = roomMapper.findRoomByOrderId(orderIdList);
            System.out.println(roomIdList);
            //批量 将拟今日入住房间状态改为已预订
            for (Room room : roomIdList) {
                //只将空闲状态的房间 改为已预订
                if (room.getStatus() == 0) {
                    roomMapper.updateStatus(3, room.getRoom_id());
                }
            }
        }
        //得到实时的 空闲状态
        List<Room> rooms = roomMapper.findRoomByStatus(0);
        return rooms;
    }
}
